package com.sanhuo.file.controller;

import com.sanhuo.file.entity.Files;
import com.sanhuo.file.service.FileService;
import com.sanhuo.lib.ExceptionUtil;
import com.sanhuo.utils.basic.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RequestMapping("file/")
@RestController
public class FileController {
    @Autowired
    private FileService fileService;

    @RequestMapping("upload")
    public Files upload(MultipartFile file) {
        ExceptionUtil.throwException(BeanUtil.isNull(file), "上传文件不能为空!");
        return fileService.upload(file);
    }

    @RequestMapping("download")
    public void downLoad(String id, HttpServletRequest req, HttpServletResponse res) throws IOException {
        fileService.downloadByFilesId(id, req, res);
    }
}
